package com.shangxia.monomer.common.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.*;

/**
 * @author zhouya38158
 * @Package com.shangxia.monomer.common.config
 * @date 2021/7/28 13:59
 */
@Configuration
@EnableOpenApi
public class Swagger2Config {

    /**
     * @param environment
     * @return
     */
    @Bean
    public Docket docketAll(Environment environment) {
        Profiles profiles = Profiles.of("dev", "test");
        boolean flag = environment.acceptsProfiles(profiles);
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .groupName("all")
                .enable(flag)
                .select()
//                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
//                .apis(RequestHandlerSelectors.withMethodAnnotation(Api.class))
                .paths(PathSelectors.any())
//                .paths(PathSelectors.ant("/log/**"))
                .build();
    }


    /**
     * log 模块接口配置
     *
     * @param environment
     * @return
     */
    @Bean
    public Docket docketLog(Environment environment) {
        Profiles profiles = Profiles.of("dev", "test");
        boolean flag = environment.acceptsProfiles(profiles);
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .groupName("log")
                .enable(flag)
                .select()
                .paths(PathSelectors.ant("/log/**"))
                .build();
    }

    /**
     * user 模块接口配置
     *
     * @param environment
     * @return
     */
    @Bean
    public Docket docketUser(Environment environment) {
        Profiles profiles = Profiles.of("dev", "test");
        boolean flag = environment.acceptsProfiles(profiles);
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .groupName("user")
                .enable(flag)
                .select()
                .paths(PathSelectors.ant("/user/**"))
                .build();
    }

    /**
     * user 模块接口配置
     *
     * @param environment
     * @return
     */
    @Bean
    public Docket docketError(Environment environment) {
        Profiles profiles = Profiles.of("dev", "test");
        boolean flag = environment.acceptsProfiles(profiles);
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .groupName("error")
                .enable(flag)
                .select()
                .paths(PathSelectors.ant("/error/**"))
                .build();
    }


    private ApiInfo apiInfo() {

        //作者信息
        Contact contact = new Contact("丄丅", "", "");
        return new ApiInfo(
                "丄丅",
                "无坚不摧",
                "1.0",
                "urn:tos",
                contact,
                "shangxia 1.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }

    /**
     * 设置授权信息
     */
    private List<SecurityScheme> securitySchemes() {
        return Collections.singletonList(new ApiKey("BASE_TOKEN", "token", "header"));
    }

    /**
     * 授权信息全局应用
     */
    private List<SecurityContext> securityContexts() {
        return Collections.singletonList(
                SecurityContext.builder()
                        .securityReferences(
                                Collections.singletonList(new SecurityReference("BASE_TOKEN",
                                        new AuthorizationScope[]{new AuthorizationScope("global", "")}
                                )))
                        .forPaths(PathSelectors.any())
                        .build()
        );
    }

    @SafeVarargs
    private final <T> Set<T> newHashSet(T... ts) {
        if (ts.length > 0) {
            return new LinkedHashSet<>(Arrays.asList(ts));
        }
        return null;
    }

}
